package edu.trinity.cs.cjenkin1.webcrawler

import scala.xml.parsing.XhtmlParser
import scala.swing._

import javax.swing.{SpinnerNumberModel, JSpinner}

object Webcrawler extends SimpleSwingApplication {

  //GUI stuffs
  val urlText = new TextField("Type URL here", 80)
  var ON_group = new ButtonGroup
  val debugLabel = new Label("Debug info printed here")

  val proxyList = new TextField("Proxies here");
  val delayDown = Component.wrap(new JSpinner(new SpinnerNumberModel(0, 0, Int.MaxValue,1)))
  
  val imageLinks = new TextArea("")
  
  val runButton = Button("Crawl webpage") {
	val status = Crawler.crawl(urlText.text, 1)
	println("Images")
	imageLinks.text = status.imagesFound.mkString("\n")
	println(imageLinks.text)
	println("Links")
	println(status.linksToFollow.mkString("\n"))
	println("Crawling finished")
	Crawler.downloadTemp(status.imagesFound(0), 
	    new Array[Byte](1024), 0, 1024)
	/*Crawler.downloadTemp(Crawler.imagesFound(0),new Array[Byte](1024),
	0,1024)*/
  }
  
  

  def top = new MainFrame {
    title = "cjenkin1's Webcrawler (subject to better name)"

    contents = new TabbedPane {
      pages += new TabbedPane.Page("Main", new BorderPanel {
        import BorderPanel.Position._

        add(urlText, North)

        add(new GridPanel(2, 3) {
          contents += new Label("# Links to Follow")
          contents += Component.wrap(
            new JSpinner(new SpinnerNumberModel(50, 1, Int.MaxValue, 1)))
          contents += new Label("Error Msg");

          //Radio buttons for filtering through URLs
          var only_never = List(new RadioButton("Only"), new RadioButton("Never"))
          ON_group.buttons ++= only_never
          ON_group.select(only_never(0))
          contents += new GridPanel(1, 2) {
            contents ++= only_never
          }
          contents += new TextField("")
          contents += new Label("Semicolon delimits")

        }, Center)

		add(runButton, South)
      })

      pages += new TabbedPane.Page("Image Gallery", imageLinks)

	  pages += new TabbedPane.Page("Network Settings", new GridPanel(2,2) {
		contents += new Label("List of proxies")
		contents += proxyList
		contents += new Label("Delay download time (in milliseconds)")
		contents += delayDown
		//add(proxyList)
	  })

      pages += new TabbedPane.Page("Debug", debugLabel)

    }
  }
}
